package com.luocj.muke.demo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class CustomIndicator extends LinearLayout {
    private int mIndicatorWidth;
    private float mRadio = 1 / 6f;
    private int mInitX;
    private int mTranslationX = 0;

    private Paint paint = new Paint();
    private Path path;


    public CustomIndicator(Context context) {
        super(context);
    }

    public CustomIndicator(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


    }

    @Override
    protected void dispatchDraw(Canvas canvas) {

        canvas.save();

        canvas.translate(mInitX + mTranslationX, getHeight());
        canvas.drawPath(path, paint);
        canvas.restore();

        super.dispatchDraw(canvas);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mIndicatorWidth = (int) (w / 3 * mRadio);
        mInitX = w / 3 / 2 - mIndicatorWidth / 2;

        init();

    }

    private void init() {

        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
        paint.setAntiAlias(true);
        paint.setPathEffect(new CornerPathEffect(3));

        float mInidcatorHeight = mIndicatorWidth / 2;
        path = new Path();
        path.moveTo(0, 0);

        path.lineTo(mIndicatorWidth, 0);
        path.lineTo(mIndicatorWidth / 2, -mInidcatorHeight);
        path.close();
    }

    public void scroll2(int position, float offset) {
        int tabWidth = getWidth()/3;
        mTranslationX = (int) (tabWidth * ( position + offset));

        invalidate();
    }
}
